<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
    <el-form-item label="状态 1正常 0冻结" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 1正常 0冻结"></el-input>
    </el-form-item>
    <el-form-item label="商户id" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商户id"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="摘要" prop="abstractInfo">
      <el-input v-model="dataForm.abstractInfo" placeholder="摘要"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="images">
      <el-input v-model="dataForm.images" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="currTime">
      <el-input v-model="dataForm.currTime" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="详细介绍" prop="desc">
      <el-input v-model="dataForm.desc" placeholder="详细介绍"></el-input>
    </el-form-item>
    <el-form-item label="点击量" prop="hits">
      <el-input v-model="dataForm.hits" placeholder="点击量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          updateTime: '',
          status: '',
          shopId: '',
          title: '',
          abstractInfo: '',
          images: '',
          currTime: '',
          desc: '',
          hits: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 1正常 0冻结不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商户id不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          abstractInfo: [
            { required: true, message: '摘要不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ],
          currTime: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          desc: [
            { required: true, message: '详细介绍不能为空', trigger: 'blur' }
          ],
          hits: [
            { required: true, message: '点击量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/testing/gallery/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.gallery.createTime
                this.dataForm.updateTime = data.gallery.updateTime
                this.dataForm.status = data.gallery.status
                this.dataForm.shopId = data.gallery.shopId
                this.dataForm.title = data.gallery.title
                this.dataForm.abstractInfo = data.gallery.abstractInfo
                this.dataForm.images = data.gallery.images
                this.dataForm.currTime = data.gallery.currTime
                this.dataForm.desc = data.gallery.desc
                this.dataForm.hits = data.gallery.hits
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/testing/gallery/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'status': this.dataForm.status,
                'shopId': this.dataForm.shopId,
                'title': this.dataForm.title,
                'abstractInfo': this.dataForm.abstractInfo,
                'images': this.dataForm.images,
                'currTime': this.dataForm.currTime,
                'desc': this.dataForm.desc,
                'hits': this.dataForm.hits
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
